<template>
  <div>
    <t-form
      ref="form"
      colon
      reset-type="initial"
      :rules="FORM_RULES"
      :data="formData"
      label-align="right"
      label-width="130px"
      @reset="onReset"
      @submit="onSubmit"
      :disabled = "formData.taskInfoType"
    >
      <t-form-item :label="$t('monitor.task.label.jobName')" name="jobName">
        <t-input-adornment :prepend="$t(formData.jobName)">
          <t-input v-model="formData.jobName" :placeholder="$t('monitor.task.fromTitle.pl.jobName')"></t-input>
        </t-input-adornment>
      </t-form-item>
      <t-form-item :label="$t('monitor.task.label.jobGroup')" name="jobGroup">
        <SelectDict
          :dictType="SYS_DICT_TYPE_JOB_GROUP"
          :placeholder="$t('monitor.task.fromTitle.pl.jobGroup')"
          v-model:value="formData.jobGroup"
        ></SelectDict>
      </t-form-item>
      <t-form-item :label="$t('monitor.task.label.cronExpression')" name="cronExpression">
        <t-input
          v-model="formData.cronExpression"
          :placeholder="$t('monitor.task.fromTitle.pl.cronExpression')"
        ></t-input>
      </t-form-item>
      <t-form-item :label="$t('monitor.task.label.invokeTarget')" name="invokeTarget">
        <t-input v-model="formData.invokeTarget" :placeholder="$t('monitor.task.fromTitle.pl.invokeTarget')"></t-input>
      </t-form-item>
      <t-form-item :label="$t('monitor.task.fromTitle.executionPolicy')" name="status">
        <SelectDict
          :dictType="SYS_JOB_MISFIRE_POLICY"
          :selectType="SelectTypeEnum.tRadio"
          v-model:value="formData.misfirePolicy"
        ></SelectDict>
      </t-form-item>
      <t-form-item :label="$t('monitor.task.fromTitle.isConcurrent')" name="status">
        <SelectDict
          :dictType="SYS_JOB_CONCURRENT"
          :selectType="SelectTypeEnum.tRadio"
          v-model:value="formData.concurrent"
        ></SelectDict>
      </t-form-item>
      <t-form-item v-if="!formData.taskInfoType">
        <t-space size="small">
          <t-button theme="primary" type="submit" :loading="loading">{{ $t('common.button.submit') }}</t-button>
          <t-button theme="default" variant="base" type="reset" :loading="loading">{{
            $t('common.button.reset1')
          }}</t-button>
        </t-space>
      </t-form-item>
    </t-form>
  </div>
</template>
<script setup lang="tsx">
import { FormRules, MessagePlugin, SubmitContext } from 'tdesign-vue-next';
import { onMounted, ref } from 'vue';

import { addTask, editTask, taskInfo } from '@/api/monitor/task';
import { Task } from '@/api/monitor/task/types';
import { DictData, DictType } from '@/api/system/dict/types';
import { ResultEnum } from '@/enums/httpEnum';
import i18n from '@/i18n';
import { useDictStore } from '@/store';
import {SYS_JOB_MISFIRE_POLICY,SYS_JOB_CONCURRENT,SYS_DICT_TYPE_JOB_GROUP} from '@/constants/dictTypes'
import SelectDict from '@/components/dict/SelectDict.vue';
import {SelectTypeEnum} from '@/components/dict/types'

const emit = defineEmits(['submit-hook']);
const FORM_RULES = ref<FormRules>({
  jobName: [{ required: true, message: i18n.global.t('dict.label.pl.dictName'), trigger: 'blur' }],
  dictType: [{ required: true, message: i18n.global.t('dict.label.pl.dictType'), trigger: 'blur' }],
});
// 表单对象
const formData = ref<Task>({
  id: '',
  jobName: '',
  jobGroup: 0,
  invokeTarget: '',
  cronExpression: '',
  misfirePolicy: 0,
  concurrent: 0,
  status: 1, taskInfoType: false
});

// 记录重置表单数据
const resetValue = ref<Task>();
const taskFromId = ref('');
const loading = ref(false);
const dictStore = useDictStore();
/**
 * 重置表单
 */
const onReset = () => {
  loading.value = true;
  if (taskFromId.value) {
    initFromData(taskFromId.value,true);
  }
  loading.value = false;
};

/**
 * 初始化表单
 */
const initFromData = async (id: string,taskInfoTask:boolean) => {
  if (!id) {
    formData.value = {
      id: '',
      jobName: '',
      jobGroup: 0,
      invokeTarget: '',
      cronExpression: '',
      misfirePolicy: 0,
      concurrent: 0,
      status: 1,
      taskInfoType:false
    };
    taskFromId.value = undefined;
    return;
  }
  taskFromId.value = id;
  const { code, result } = await taskInfo(id);
  if (ResultEnum.SUCCESS === code) {
    if(taskInfoTask){
      result.taskInfoType = true;
    }
    formData.value = result;
    resetValue.value = result;
  }
};

/**
 * 表单提交事件
 * @param param0 表单验证
 */
const onSubmit = async ({ validateResult }: SubmitContext<DictType>) => {
  if (validateResult === true) {
    loading.value = true;
    const api = formData.value.id ? editTask : addTask;
    const res = await api(formData.value);
    if (res.code === ResultEnum.SUCCESS) {
      MessagePlugin.success(i18n.global.t('common.message.submitSuccess'));
      emit('submit-hook');
    } else {
      MessagePlugin.error(res.message);
    }
    loading.value = false;
  }
};

onMounted(async () => {
});

defineExpose({
  initFromData,
});
</script>

<style lang="less" scoped>
.treeBox {
  border: 1px solid #ddd;
  overflow: auto;
  height: 300px;
  width: 100%;
}
</style>
@/components/selectDict/types